var basePath = $("#basePath").val();
$(document).ready(function (){
    menuCss("设备管理");
    chart();
    queryDeviceInfo();
});

function chart(){
    var chartA = echarts.init(document.getElementById('chartA'),'macarons');
    var optionA = {
        tooltip : {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        legend: {
            orient: 'vertical',
            left: 'left',
            data: ['维修次数','保养次数','保养计划次数']
        },
        series : [
            {
                type: 'pie',
                radius : '55%',
                center: ['50%', '60%'],
                data:[
                    {value:0, name:'维修次数'},
                    {value:0, name: '保养计划次数'},
                    {value: 0, name:'保养次数'},
                ],
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    };
    chartA.setOption(optionA);
}


function queryDeviceInfo(){
    var deviceId = $("#deviceId").val();
    var url = basePath+"/device/queryDeviceInfo";
    publicAjax(url,"POST",{deviceId:deviceId},function (data) {
             if(data.ret == 200){
                 bindDeviceInfo(data.data);
             }else{
                 flavrShowByTime(data.msg,null,"danger",false);
             }
    });
}

function queryContractInfo(id){
    var url = basePath+"/contract/info?id="+id;
    location.href=url;
}

function bindDeviceInfo(obj){
        $("#deviceNameSpan").html(obj.deviceName == null ? '' : obj.deviceName);
        $("#deviceModelSpan").html(obj.deviceModel == null ? '' : obj.deviceModel);
        $("#snSpan").html(obj.sn == null ? '' : obj.sn);
        $("#hospitalNameSpan").html(obj.hospitalName == null ? '' : obj.hospitalName);
        $("#manufacturerNameSpan").html(obj.manufacturerName == null ? '' : obj.manufacturerName);
        $("#maintainerNameSpan").html(obj.maintainerName == null ? '' : obj.maintainerName);
        $("#supplierNameSpan").html(obj.supplierName == null ? '' : obj.supplierName);
        $("#departmentSpan").html(obj.department == null ? '' : obj.department);
        $("#assetsNoSpan").html(obj.assetsNo == null ? '' : obj.assetsNo);
        $("#placeOriginSpan").html(obj.placeOrigin == null ? '' : obj.placeOrigin);
        $("#specificationsSpan").html(obj.specifications == null ? '' : obj.specifications);
        $("#priceSpan").html(obj.price == null ? '' : obj.price/100);
        $("#quantitySpan").html(obj.quantity == null ? 0 : obj.quantity);
        $("#attachmentSpan").html(obj.attachment == null ? '' : obj.attachment);
        $("#upkeepCycleSpan").html(obj.upkeepCycle == null ? '' : obj.upkeepCycle);
        $("#lastUpkeepDateSpan").html(obj.lastUpkeepDate == null ? '' : obj.lastUpkeepDate);
        $("#installDateSpan").html(obj.installDate == null ? '' : obj.installDate);
        $("#acceptanceDateSpan").html(obj.acceptanceDate == null ? '' : obj.acceptanceDate);
        $("#purchaseDateSpan").html(obj.purchaseDate == null ? '' : obj.purchaseDate);
        $("#manufactureDateSpan").html(obj.manufactureDate == null ? '' : obj.manufactureDate);
        $("#enableTimeSpan").html(obj.enableTime == null ? '' : obj.enableTime);
        $("#responsiblePersonSpan").html(obj.responsiblePerson == null ? '' : obj.responsiblePerson);
        $("#deviceStatusItemSpan").html(obj.deviceStatusItem == null ? '' : obj.deviceStatusItem);
        $("#deviceTypeNameSpan").html(obj.deviceTypeName == null ? '' : obj.deviceTypeName);
        $("#deviceTypeSubNameSpan").html(obj.deviceTypeSubName == null ? '' : obj.deviceTypeSubName);
        //$("#contractNoSpan").html(obj.contractNo == null ? '' : obj.contractNo);
        $("#remarkSpan").html(obj.remark == null ? '' : obj.remark);
        $("#nextUpkeepDateSpan").html(obj.nextUpkeepDate == null ? '' : obj.nextUpkeepDate);

        //绑定合同信息
        if(obj.contractView != null){
            var html1="<a href = \"javascript:queryContractInfo("+obj.contractView.contractId+")\">"+(obj.contractView.contractNo == null ? '' : obj.contractView.contractNo) +"</a>"
            $(".medicalSecuritySpan").html(obj.contractView.medicalSecurity == null ? '' : obj.contractView.medicalSecurity );
            $("#contractNoSpan").html(html1);
            $("#firstPartyNameSpan").html(obj.contractView.hospitalName == null ? '' : obj.contractView.hospitalName );
            $("#secondPartyNameSpan").html(obj.contractView.manufacturerName == null ? '' : obj.contractView.manufacturerName );
            $("#signDateSpan").html(obj.contractView.signDate == null ? '' : obj.contractView.signDate );
            $("#deviceWarrantyPeriodSpan").html(obj.contractView.deviceWarrantyPeriod == null ? '' : obj.contractView.deviceWarrantyPeriod );
            $("#supplierWarrantyPeriodSpan").html(obj.contractView.supplierWarrantyPeriod == null ? '' : obj.contractView.supplierWarrantyPeriod );

            $(".manufacturerLastDateSpan").html(obj.contractView.manufacturerLastDate == null ? '' : obj.contractView.manufacturerLastDate );
            $(".supplierLastDateSpan").html(obj.contractView.supplierLastDate == null ? '' : obj.contractView.supplierLastDate );

            $("#contractRemarkSpan").html(obj.contractView.remark == null ? '' : obj.contractView.remark );
            //$("#aaaSpan").html(obj.contractView.guaranteeLastDate == null ? '': obj.contractView.guaranteeLastDate );
        }else{
            $(".contract").parent().append('<tr><td class="text-center" colspan="5"><label class="text-muted">暂无合同信息</label></td></tr>');
            $(".contract").remove();
        }


        //绑定事件
        var str = "";
        if(obj.eventList == null || obj.eventList.length == 0){
            str = "暂无事件";
            $("#timeline").html(str);
        }else{
            $.each(obj.eventList,function (index,item) {
                    var classs="";
                    var color="";
                    switch (item.type){
                        case 1700 :classs="fa fa-wrench";
                            color = "#f9b229";
                            break;
                        case 1701 :classs="fa fa-cogs";
                            color = "#82ca2b";
                            break;
                        case 1702 :classs="fa fa-rocket";
                            color = "#10458f";
                            break;
                        default : classs="fa fa-clock-o";
                            color="#5082eb";
                    }

                str+="<li><i class='"+classs+"' style='color: #fff;background:"+color+"'></i>"
                    +"<div class='timeline-item'>"
                    +"<h3 class='timeline-header' style='background: #fff'>"
                    +"<span style='color: #000'>"+(item.eventDateTime==null?'':item.eventDateTime)+"</span>"
                    +"</h3>"
                    +"<div class='timeline-body'>"
                    +"<span style='color: #5082eb'>"+(item.eventTitle==null?'': '【'+item.eventTitle+'】 ')+"</span>"
                    +item.eventContent
                    +"</div>"
                    +"<div>";

                    if(item.fileList != null && item.fileList.length > 0){
                        $.each(item.fileList,function (index,items) {
                            str+= "<img class='imgList' style = 'width:100px;height:30px;' src='"+items.fileUrl+"' alt='Visa'> &nbsp;";
                        })
                    }
                str+="</div>"
                    +"</div>"
                    +"</li>";
            });
            str+="<li><i class='fa fa-clock-o bg-gray'></i></li>";
            $("#timeAxis").html(str);

            if($(".imgList").length > 0){
                $(".imgList").viewer("destroy");
                $(".imgList").viewer();
            }
        }


        //绑定文件信息
        $("#device_file_list").html("");
        var strs = "";
        if(obj.fileList == null || obj.fileList.length == 0){
            strs = "<tr><td colspan='7' class='text-muted text-center'>暂无文件</td></tr>";
        }else{
            $.each(obj.fileList,function (index,item) {
                var fileName = item.fileName + "." + item.fileSuffix;
                strs += "<tr><td class='text-muted text-center'>"+(index+1)+"</td>" +
                    "<td class='text-muted text-center'>"+(item.fileTypeName == null?'':item.fileTypeName)+"</td>" +
                    "<td class='text-muted text-center'><a href='javascript:showFile(\""+item.fileUrl+"\",\""+fileName+"\")'>"+(item.fileName == null?'':item.fileName)+"</a></td>" +
                    "<td class='text-muted text-center'>"+(item.startDate == null?'':item.startDate)+"</td>" +
                    "<td class='text-muted text-center'>"+(item.endDate == null?'':item.endDate)+"</td>" +
                    "<td class='text-muted text-center'>"+(item.fileSize == null?'':item.fileSize)+"</td>" +
                    "<td class='text-muted text-center'><button type='button' class='btn btn-danger btn-xs' title='下载' onclick='fileDownload(\""+item.fileName+"\",\""+item.fileUrl+"\",\""+item.fileSuffix+"\")'><i class='fa fa-fw fa-download'></i></button></td>" +
                    "</tr>";
            });
        }
        $("#device_file_list").html(strs);

    //绑定维修历史信息
    $("#device_repair_list").html("");
    var strs = "";
    if(obj.repairRecords == null || obj.repairRecords.length == 0){
        strs = "<tr><td colspan='5' class='text-muted text-center'>暂无维修记录</td></tr>";
    }else{
        $.each(obj.repairRecords,function (index,item) {
            var fileName = item.fileName + "." + item.fileSuffix;
            strs += "<tr><td class='text-muted text-center'>"+(index+1)+"</td>" +
                "<td class='text-muted text-center'><button type='button' class='btn btn-primary btn-xs' onclick='queryRepairInfo("+item.repairId+")' data-original-title='详情'><i class='fa fa-file-text-o'></i></button>" +
                "<td class='text-muted text-center'>"+(item.faultTime == null?'':item.faultTime)+"</td>" +
                "<td class='text-muted text-center'>"+(item.repairTime == null?'':item.repairTime)+"</td>" +
                "<td class='text-muted text-center'>"+(item.statusStr == null?'':item.statusStr)+"</td>" +
                "</tr>";
        });
    }
    $("#device_repair_list").html(strs);
}

function queryRepairInfo(id){
    location.href = basePath+"/repair/info?id="+id;
}

function showFile(url,file){
    $("#file_show_modal").iziModal({
        zindex:999999,
        headerColor: '#00c0ef',
        title: '预览',
        subtitle: file,
        overlayClose: true,
        history:false,
        iframe : true,
        iframeURL:url,
        fullscreen: true,
        openFullscreen: false,
        onClosed:function(){
            $('#file_show_modal').iziModal('destroy');
        }
    });
    $("#file_show_modal").iziModal('open');
}

function fileDownload(fileName,filePath,fileSuffix){
    location.href= basePath+"/file/fileDownload?filePath="+filePath+"&fileName="+fileName;
}


function showService(type){
    if(type == 1){
        $("#clickRepairCount-table").show();
        $("#clickUpkeepCount-table").hide();
        $("#clickUpkeepPlanCount-table").hide();
        $("#list-model").iziModal("setTitle","维修列表");
    }else if(type == 2){
        $("#clickRepairCount-table").hide();
        $("#clickUpkeepCount-table").show();
        $("#clickUpkeepPlanCount-table").hide();
        $("#list-model").iziModal("setTitle","保养列表");
    }else if(type==3){
        $("#clickRepairCount-table").hide();
        $("#clickUpkeepCount-table").hide();
        $("#clickUpkeepPlanCount-table").show();
        $("#list-model").iziModal("setTitle","保养计划列表");
    }
    $("#list-model").iziModal("open",this);
}

$("#list-model").iziModal({
    title: "列表",
    headerColor: '#00c0ef',
    rtl: false,
    width: 650,
    zindex: 1042,
    focusInput: false,
    group: '',
    loop: false,
    history: false,
    overlayClose: false,
    autoOpen: false, // Boolean, Number
    bodyOverflow: true,
    onOpening: function () {},
    onOpened: function () {},
    onClosing: function () {},
    onClosed: function () {}
});